﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIM.controller;
using LIM.model;

namespace LIM.forms.uc_DanhMuc_ChiTieu
{
    public partial class uc_DanhMuc_ChiTieu_BangGia : UserControl
    {
        DanhMucChiTieuController controller = new DanhMucChiTieuController();
        public uc_DanhMuc_ChiTieu_BangGia()
        {
            InitializeComponent();
            BindingCompany.DataSource = controller.LoadKhachHang();
        }
        radForm_DanhMuc_ChiTieu parentForm = null;
        void parentForm_OnRowChange()
        {
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }
        }
        void LoadData()
        {
            if (parentForm.SelectedEntity.RowID != 0)
            {
                radGridView1.DataSource = controller.LoadBangGiaByRowIDChiTieu(parentForm.SelectedEntity.RowID);
            }
            else
            {
                radGridView1.DataSource = new List<tbl_DanhMuc_ChiTieu_BangGia>();
            }
        }

        void Save()
        {
                for (int i = 0; i < radGridView1.Rows.Count(); i++)
			    {
                    tbl_DanhMuc_ChiTieu_BangGia item = (tbl_DanhMuc_ChiTieu_BangGia)radGridView1.Rows[i].DataBoundItem;
                    if (item != null)
                    {
                        if (item.Gia != null && item.NgayHieuLuc != null)
                        {
                            item.RowIDChiTieu = parentForm.SelectedEntity.RowID;
                            if (item.RowID == 0)
                            {
                                parentForm.SelectedEntity.NguoiTao = InitUserInfos.UserName;
                                item.NgayTao = item.NgayCapNhat = parentForm.controller.GetCurrentDateTimeFromDatabaseServer();
                            }
                            else
                            {
                                item.NguoiCapNhat = InitUserInfos.UserName;
                                item.NgayCapNhat = parentForm.controller.GetCurrentDateTimeFromDatabaseServer();
                            }
                            parentForm.controller.SaveBangGia_ChiTieu(item);
                        }
                    }
                }
                RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Bảng giá Chỉ tiêu", "Đã lưu thành công!");
                parentForm.RefreshData();
                if (parentForm.SelectedEntity != null)
                {
                    LoadData();
                }
        }
        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void uc_DanhMuc_ChiTieu_BangGia_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_DanhMuc_ChiTieu)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnRowChange += new radForm_DanhMuc_ChiTieu.GridViewDoubleClick(parentForm_OnRowChange);
            }
            if (parentForm.SelectedEntity != null)
            {
                LoadData();
            }
        }
    }
}
